/*
 * MainFrame.java
 *
 * Created on December 1, 2006, 10:12 PM
 */

package eu.somatik.somaftp.gui;

import eu.somatik.somaftp.SomaFTP;
import eu.somatik.somaftp.logic.FileSystemInterface;
import eu.somatik.somaftp.logic.TransferQueue;
import java.awt.EventQueue;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author  francisdb
 */
public class MainFrame extends javax.swing.JFrame {
    
    private FileSystemInterface connection;
    
    private SomaFTP somaFTP;
    
    /** Creates new form MainFrame */
    public MainFrame(SomaFTP somaFTP) {
        this.somaFTP = somaFTP;
        this.connection = somaFTP.getFileSystem1();
        initComponents();
        DefaultListModel model = new DefaultListModel();
        logList.setModel(model);
        model.addListDataListener(new ListDataListener() {
            public void contentsChanged(ListDataEvent e) {
                
            }
            public void intervalAdded(ListDataEvent e) {
                int lastIndex = logList.getModel().getSize() - 1;
                if (lastIndex >= 0) {
                    logList.ensureIndexIsVisible(lastIndex);
                }
            }
            public void intervalRemoved(ListDataEvent e) {
            }
        });
        
        leftFileSystemPanel.setMainFrame(this);
        rightFileSystemPanel.setMainFrame(this);
        
        leftFileSystemPanel.setFileSystem(somaFTP.getFileSystem1());
        rightFileSystemPanel.setFileSystem(somaFTP.getFileSystem2());
        
        queueTable.setFillsViewportHeight(true);
        queueTable.setModel(somaFTP.getQueue());
        
        // center on screen
        setLocationRelativeTo(null);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        verticalSplitpane = new javax.swing.JSplitPane();
        topSplitPane = new javax.swing.JSplitPane();
        leftFileSystemPanel = new eu.somatik.somaftp.gui.FileSystemPanel();
        rightFileSystemPanel = new eu.somatik.somaftp.gui.FileSystemPanel();
        bottomSplitPane = new javax.swing.JSplitPane();
        queueScrollPane = new javax.swing.JScrollPane();
        queueTable = new javax.swing.JTable();
        logScrollPane = new javax.swing.JScrollPane();
        logList = new javax.swing.JList();
        mainMenuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        exitMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        AboutMenuItem = new javax.swing.JMenuItem();
        transferMenu = new javax.swing.JMenu();
        startMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("FlashFTP");
        verticalSplitpane.setBorder(null);
        verticalSplitpane.setDividerLocation(300);
        verticalSplitpane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        verticalSplitpane.setResizeWeight(0.5);
        topSplitPane.setDividerLocation(350);
        topSplitPane.setDividerSize(5);
        topSplitPane.setResizeWeight(0.5);
        topSplitPane.setLeftComponent(leftFileSystemPanel);

        topSplitPane.setRightComponent(rightFileSystemPanel);

        verticalSplitpane.setLeftComponent(topSplitPane);

        bottomSplitPane.setBorder(null);
        bottomSplitPane.setDividerLocation(350);
        bottomSplitPane.setDividerSize(5);
        bottomSplitPane.setResizeWeight(0.5);
        queueTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        queueTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        queueScrollPane.setViewportView(queueTable);

        bottomSplitPane.setLeftComponent(queueScrollPane);

        logList.setFont(new java.awt.Font("DialogInput", 0, 10));
        logList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Ready to connect..." };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        logScrollPane.setViewportView(logList);

        bottomSplitPane.setRightComponent(logScrollPane);

        verticalSplitpane.setRightComponent(bottomSplitPane);

        fileMenu.setMnemonic('F');
        fileMenu.setText("File");
        fileMenu.setToolTipText("File menu");
        exitMenuItem.setMnemonic('E');
        exitMenuItem.setText("Exit");
        exitMenuItem.setToolTipText("Quits this application");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });

        fileMenu.add(exitMenuItem);

        mainMenuBar.add(fileMenu);

        helpMenu.setMnemonic('H');
        helpMenu.setText("Help");
        helpMenu.setToolTipText("Help menu");
        AboutMenuItem.setMnemonic('A');
        AboutMenuItem.setText("About");
        AboutMenuItem.setToolTipText("About dialog");
        AboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AboutMenuItemActionPerformed(evt);
            }
        });

        helpMenu.add(AboutMenuItem);

        mainMenuBar.add(helpMenu);

        transferMenu.setMnemonic('T');
        transferMenu.setText("Transfer");
        startMenuItem.setMnemonic('S');
        startMenuItem.setText("Start");
        startMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startMenuItemActionPerformed(evt);
            }
        });

        transferMenu.add(startMenuItem);

        mainMenuBar.add(transferMenu);

        setJMenuBar(mainMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(verticalSplitpane, javax.swing.GroupLayout.DEFAULT_SIZE, 751, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(verticalSplitpane, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void startMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startMenuItemActionPerformed
        somaFTP.startTransfers();
    }//GEN-LAST:event_startMenuItemActionPerformed

    private void AboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AboutMenuItemActionPerformed
        new AboutDialog(this, true).setVisible(true);
    }//GEN-LAST:event_AboutMenuItemActionPerformed
            
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        dispose();
    }//GEN-LAST:event_exitMenuItemActionPerformed

    
    protected DefaultListModel getLogModel(){
        return (DefaultListModel)logList.getModel();
    }
    
    protected void showError(Throwable ex){
        ex.printStackTrace();
        ExceptionShower shower = new ExceptionShower(ex.getMessage());
        if(EventQueue.isDispatchThread()){
            shower.run();
        }else{
            SwingUtilities.invokeLater(shower);
        }
    }
    
    private class ExceptionShower implements Runnable{
        private String message;
        private ExceptionShower(String message){
            this.message = message;
        }
        public void run() {
            JOptionPane.showMessageDialog(MainFrame.this, message, "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }
        
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem AboutMenuItem;
    private javax.swing.JSplitPane bottomSplitPane;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private eu.somatik.somaftp.gui.FileSystemPanel leftFileSystemPanel;
    private javax.swing.JList logList;
    private javax.swing.JScrollPane logScrollPane;
    private javax.swing.JMenuBar mainMenuBar;
    private javax.swing.JScrollPane queueScrollPane;
    private javax.swing.JTable queueTable;
    private eu.somatik.somaftp.gui.FileSystemPanel rightFileSystemPanel;
    private javax.swing.JMenuItem startMenuItem;
    private javax.swing.JSplitPane topSplitPane;
    private javax.swing.JMenu transferMenu;
    private javax.swing.JSplitPane verticalSplitpane;
    // End of variables declaration//GEN-END:variables
    
}
